﻿namespace PDTPublisher
{
    using PDTPublisher.Properties;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.IO;
    using System.Windows.Forms;

    public class MainForm : Form
    {
        private IContainer components = null;
        private Label label1;
        private Label label2;
        private Label label3;
        private Label label4;
        private TextBox NewPDTVersionTextBox;
        private Label OldPDTVersionLabel;
        private Button PublicButton;
        private TextBox SourceTextBox;
        private TextBox TargetTextBox;
        private NotifyIcon notifyIcon;

        public MainForm()
        {
            this.InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.label1 = new Label();
            this.OldPDTVersionLabel = new Label();
            this.label2 = new Label();
            this.NewPDTVersionTextBox = new TextBox();
            this.SourceTextBox = new TextBox();
            this.TargetTextBox = new TextBox();
            this.PublicButton = new Button();
            this.label3 = new Label();
            this.label4 = new Label();
            this.notifyIcon = new NotifyIcon();
            base.SuspendLayout();
            this.label1.AutoSize = true;
            this.label1.Location = new Point(12, 0x24);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x7d, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "Current PDT Version:";
            this.OldPDTVersionLabel.AutoSize = true;
            this.OldPDTVersionLabel.Location = new Point(0x8f, 0x24);
            this.OldPDTVersionLabel.Name = "OldPDTVersionLabel";
            this.OldPDTVersionLabel.Size = new Size(0x23, 12);
            this.OldPDTVersionLabel.TabIndex = 1;
            this.OldPDTVersionLabel.Text = "0.0.0";
            this.label2.AutoSize = true;
            this.label2.Location = new Point(0x24, 0x43);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x65, 12);
            this.label2.TabIndex = 2;
            this.label2.Text = "New PDT Version:";
            this.NewPDTVersionTextBox.Location = new Point(0x8f, 0x40);
            this.NewPDTVersionTextBox.Name = "NewPDTVersionTextBox";
            this.NewPDTVersionTextBox.Size = new Size(0x89, 0x15);
            this.NewPDTVersionTextBox.TabIndex = 3;
            this.SourceTextBox.Location = new Point(0x40, 0x77);
            this.SourceTextBox.Name = "SourceTextBox";
            this.SourceTextBox.Size = new Size(0xd8, 0x15);
            this.SourceTextBox.TabIndex = 4;
            this.SourceTextBox.MouseClick += new MouseEventHandler(this.SourceTextBoxMouseClick);
            this.TargetTextBox.Location = new Point(0x40, 0x92);
            this.TargetTextBox.Name = "TargetTextBox";
            this.TargetTextBox.Size = new Size(0xd8, 0x15);
            this.TargetTextBox.TabIndex = 5;
            this.TargetTextBox.MouseClick += new MouseEventHandler(this.TargetTextBoxMouseClick);
            this.PublicButton.Location = new Point(0xcd, 0xb8);
            this.PublicButton.Name = "PublicButton";
            this.PublicButton.Size = new Size(0x4b, 0x17);
            this.PublicButton.TabIndex = 6;
            this.PublicButton.Text = "Publish";
            this.PublicButton.UseVisualStyleBackColor = true;
            this.PublicButton.Click += new EventHandler(this.PublicButtonClick);
            this.label3.AutoSize = true;
            this.label3.Location = new Point(12, 0x7a);
            this.label3.Name = "label3";
            this.label3.Size = new Size(0x2f, 12);
            this.label3.TabIndex = 7;
            this.label3.Text = "Source:";
            this.label4.AutoSize = true;
            this.label4.Location = new Point(11, 0x95);
            this.label4.Name = "label4";
            this.label4.Size = new Size(0x2f, 12);
            this.label4.TabIndex = 8;
            this.label4.Text = "Target:";
            this.notifyIcon.DoubleClick += new EventHandler(NotifyIconDoubleClick);
            this.notifyIcon.BalloonTipText = "发布PDT";
            this.notifyIcon.BalloonTipTitle = "Lance";
            this.notifyIcon.Visible = true;
            this.notifyIcon.Icon = Icon;
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x124, 210);
            base.Controls.Add(this.label4);
            base.Controls.Add(this.label3);
            base.Controls.Add(this.PublicButton);
            base.Controls.Add(this.TargetTextBox);
            base.Controls.Add(this.SourceTextBox);
            base.Controls.Add(this.NewPDTVersionTextBox);
            base.Controls.Add(this.label2);
            base.Controls.Add(this.OldPDTVersionLabel);
            base.Controls.Add(this.label1);
            base.Name = "MainForm";
            this.Text = "PDT Publish Tool";
            base.Load += new EventHandler(this.MainFormLoad);
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void NotifyIconDoubleClick(object sender, EventArgs e)
        {
            Deploy();
            MainFormLoad(this,new EventArgs());
            notifyIcon.ShowBalloonTip(2000);
        }

        private void MainFormLoad(object sender, EventArgs e)
        {
            this.OldPDTVersionLabel.Text = Utility.GetCurrentPDTVersion();
            this.NewPDTVersionTextBox.Text = this.OldPDTVersionLabel.Text;
            this.SourceTextBox.Text = Settings.Default.SourceFolder;
            this.TargetTextBox.Text = Settings.Default.TargetFolder;
        }

        private void PublicButtonClick(object sender, EventArgs e)
        {
            Deploy(NewPDTVersionTextBox.Text);
        }

        public static void Deploy()
        {
            string[] v = Utility.GetCurrentPDTVersion().Split('.');
            v[v.Length - 1] = (int.Parse(v[v.Length - 1]) + 1).ToString();

            string sb = "";
            foreach (var s in v)
            {
                sb += s + ".";
            }
            sb = sb.TrimEnd('.');

            Deploy(sb);
        }

        public static void Deploy(string newVersion)
        {
            try
            {
                var fileExtensions = Settings.Default.IncludeFileExtensions.Split(',');

                foreach (var fileExtension in fileExtensions)
                {
                    var files = Directory.GetFiles(Settings.Default.SourceFolder, string.Format("*.{0}", fileExtension));
                    foreach (var str in files)
                    {
                        var info = new FileInfo(str);
                        File.Copy(str, string.Format("{0}//{1}", Settings.Default.TargetFolder, info.Name), true);
                    }
                }

                Utility.UpgradePDTVersion(newVersion);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        private void SourceTextBoxMouseClick(object sender, MouseEventArgs e)
        {
            var dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog(this) == DialogResult.OK)
            {
                this.SourceTextBox.Text = dialog.SelectedPath;
                Settings.Default["SourceFolder"] = this.SourceTextBox.Text;
                Settings.Default.Save();
            }
        }

        private void TargetTextBoxMouseClick(object sender, MouseEventArgs e)
        {
            var dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog(this) == DialogResult.OK)
            {
                this.TargetTextBox.Text = dialog.SelectedPath;
                Settings.Default["TargetFolder"] = this.TargetTextBox.Text;
                Settings.Default.Save();
            }
        }
    }
}

